<?php
/**
 * @Author:      lyt
 * @DateTime:    2017-02-23 12:11:08
 * @Description: 订单的检测的详情
 */
class OrderDetection extends Eloquent
{
    protected $table = "order_detection";

    /**
     * @Author:      lyt
     * @DateTime:    2017-02-23 14:24:26
     * @Description: 传入 检测结果 检测增值项目 授权人呢 技师信息 和总评
     */
    public static function savedate($de,$di,$engineer_data,$auditor,$order_id,$desc){
    	return DB::transaction(function () use ($de,$di,$engineer_data,$auditor,$order_id,$desc) {
    		OrderDetection::where("order_id",$order_id)->delete();
    	    foreach ($de as $_de) {
    	    	$one_od=new OrderDetection;
    	    	$one_od->order_id=$order_id;
    	    	$one_od->name=$_de->name;
    	    	$one_od->c_name=$_de->c_name;
    	    	$one_od->parent_id=0;
    	    	$one_od->dt_id=$_de->id;
    	    	$one_od->level=1;
    	    	$one_od->c_desc=$_de->c_desc;
    	    	$one_od->fraction=$_de->fraction;
    	    	$one_od->status=$_de->check_status;
    	    	$one_od->sort=$_de->sort;
    	    	$one_od->create_by=$engineer_data->id;
    	    	$one_od->save();
    	    	foreach ($_de->item_arr as $_two_de) {
    	    		$two_od=new OrderDetection;
	    	    	$two_od->order_id=$order_id;
	    	    	$two_od->name=$_two_de->name;
	    	    	$two_od->c_name=$_two_de->c_name;
	    	    	$two_od->parent_id=$one_od->id;
	    	    	$two_od->dt_id=$_two_de->id;
	    	    	$two_od->level=2;
	    	    	$two_od->c_desc=$_two_de->c_desc;
	    	    	$two_od->fraction=$_two_de->fraction;
	    	    	$two_od->status=$_two_de->check_status;
	    	    	$two_od->sort=$_two_de->sort;
	    	    	$two_od->create_by=$engineer_data->id;
	    	    	$two_od->save();
	    	    	foreach ($_two_de->item_arr as $_thr_de) {
	    	    		$three_od=new OrderDetection;
		    	    	$three_od->order_id=$order_id;
		    	    	$three_od->name=$_thr_de->name;
		    	    	$three_od->c_name=$_thr_de->c_name;
		    	    	$three_od->parent_id=$two_od->id;
		    	    	$three_od->dt_id=$_thr_de->id;
		    	    	$three_od->level=3;
		    	    	$three_od->c_desc=$_thr_de->c_desc;
		    	    	$three_od->fraction=$_thr_de->fraction;
		    	    	$three_od->status=$_thr_de->check_status;
		    	    	$three_od->sort=$_thr_de->sort;
		    	    	$three_od->create_by=$engineer_data->id;
		    	    	$three_od->save();
	    	    	}
    	    	}
    	    }
    	    OrderDetectionIncrement::where("order_id",$order_id)->delete();
    	    foreach ($di as $_di) {
    	    	$di_info=new OrderDetectionIncrement;
    	    	$di_info->order_id=$order_id;
    	    	$di_info->status=$_di->check_status;
    	    	$di_info->sort=$_di->sort;
    	    	$di_info->di_id=$_di->id;
    	    	$di_info->di_name=$_di->name;
    	    	$di_info->create_by=$engineer_data->id;
    	    	$di_info->save();    	    	
    	    }
    	    OrderDetectionDesc::where("order_id",$order_id)->delete();
    	    $odc=new OrderDetectionDesc;
    	    $odc->order_id=$order_id;
    	    $odc->note=$desc;
    	    $odc->auth_by=$auditor->id;
    	    $odc->create_by=$engineer_data->id;
    	    $odc->save();
            return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_id));
        });
    }


    public static function getOrderDetection($order_id){
        $de=OrderDetection::where("order_id",$order_id)->where("level",1)->orderBy("sort","asc")->select("id","order_id","name","c_name","parent_id","dt_id","level","amount","c_desc","fraction","status","sort")->get();
        $order = Orders::where('order_id',$order_id)->first();
        $exception=array(); 
        $exception_amount=0;
        $i = 0;
        foreach ($de as $_de) {
            // $_de->amount=$_de->amount/100;
            $two_de=OrderDetection::where("order_id",$order_id)->where("parent_id",$_de->id)->where("level",2)->select("id","order_id","name","c_name","parent_id","dt_id","level","amount","c_desc","fraction","status","sort")->orderBy("sort","asc")->get();
            $i +=1;
            foreach ($two_de as $_t_de) {
                // $_t_de->amount=$_t_de->amount/100;
                if($_t_de->status==1||$_t_de->status==2){//说明异常了 或者异常修复了
                   $temp= clone $_t_de;
                   $two_temp=OrderDetection::where("order_id",$order_id)->where("parent_id",$_t_de->id)->where("level",3)->where("status",1)->select("id","order_id","name","c_name","parent_id","dt_id","level","amount","c_desc","fraction","status","sort")->orderBy("sort","asc")->get();
                   // foreach ($two_temp as $value) {
                   //     $value->amount=$value->amount/100;
                   // }
                   $temp->item_arr=$two_temp;
                    try{
                        //默认图片
                        $checkreport_img_user_id = Config::get('config.checkreport_img_user_id');
                        if(in_array($order->user_id,$checkreport_img_user_id) && !is_null($temp) && $i == 1){
                            $temp->report_img = Config::get('config.checkreport_img_url');
                        }
                    }catch(Exception $e){
                        $response = array('code' => -1, 'msg' => '获取检测报告详情图片失败');
                        break;
                    }
                   $exception[]=$temp;
                   $exception_amount+=$temp->amount;
                }
                $two_temp2=OrderDetection::where("order_id",$order_id)->where("parent_id",$_t_de->id)->where("level",3)->select("id","order_id","name","c_name","parent_id","dt_id","level","amount","c_desc","fraction","status","sort")->orderBy("sort","asc")->get();
                // foreach ($two_temp2 as $value) {
                //     $value->amount=$value->amount/100;
                // }
                $_t_de->item_arr=$two_temp2;
            }
            $_de->item_arr=$two_de;
        }
        $odi=OrderDetectionIncrement::where("order_id",$order_id)->select("id","order_id","order_id","status","sort","di_id","di_name")->orderBy("sort","asc")->get();
        $odd =OrderDetectionDesc::leftJoin("maintenance_engineers as me1","order_detection_desc.create_by","=","me1.id")->leftJoin("maintenance_engineers as me2","order_detection_desc.auth_by","=","me2.id")->select("order_detection_desc.note","order_detection_desc.create_by","me1.name as create_name","order_detection_desc.auth_by","me2.name as auth_name","order_detection_desc.created_at")->where("order_detection_desc.order_id",$order_id)->first();
        return array("check"=>$de,"exception_check"=>$exception,"check_product"=>$odi,"check_desc"=>isset($odd)?$odd:(object)null,"exception_amount"=>$exception_amount);
    }
}